package com.hongkuncheng.cms.controller.member;

import java.io.IOException;

import com.hongkuncheng.cms.helper.SecurityHelper;
import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Controller;
import com.hongkuncheng.cms.controller.BaseController;
import com.hongkuncheng.cms.helper.CookieHelper;
import com.hongkuncheng.cms.helper.PageHelper;
import com.hongkuncheng.cms.helper.StringHelper;
import com.hongkuncheng.cms.model.Member;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@Transactional
@RequestMapping("member")
public class MindexController extends BaseController {

    // 会员中心
    @RequestMapping
    public String index() {
        frontpageCommon();
        request.setAttribute("viewcontentpath", webinfo.getTemplate() + "/member.html");
        return view();
    }

    // 登录
    @RequestMapping("login")
    public String login() {
        String mobile = CookieHelper.GetCookie(request, "member_mobile", true);
        if (mobile != null && !mobile.isEmpty()) {
            request.setAttribute("mobile", mobile);
            request.setAttribute("password", CookieHelper.GetCookie(request,
                    "member_password_" + mobile, true));
        }
        frontpageCommon();
        System.out.println("webinfo.getTemplate() + \"/member_login.html\":" + webinfo.getTemplate() + "/member_login.html");
        request.setAttribute("viewcontentpath", webinfo.getTemplate() + "/member_login.html");
        return view();
    }

    // 登录处理
    @RequestMapping("logindo")
    public void logindo() throws Exception {
        String checkcode = CookieHelper.GetCookie(request, "checkcode", true);
        if (checkcode == null) {
            PageHelper.alertAndRedirect("验证码加载失败", "/member/login");
        } else {
            String vcode = request.getParameter("vcode");
            if (!vcode.equalsIgnoreCase(checkcode)) {
                PageHelper.alertAndGoBack("验证码错误，请重新输入。");
            } else {
                String mobile = request.getParameter("mobile");
                String password = request.getParameter("password");
                if (mobile == null || password == null) {
                    response.sendRedirect("/errors/500.html");
                } else {
                    Member member = (Member) factorySession.createCriteria(Member.class)
                            .add(Restrictions.eq("mobile", mobile))
                            .uniqueResult();
                    if (member != null) {
                        if (!member.getPassword().equals(SecurityHelper.md5Encrypt(mobile + "_" + password))) {
                            PageHelper.alertAndGoBack("手机号或密码错误，请重新输入。");
                            return;
                        }
                    } else {
                        member = new Member();
                        member.setMobile(mobile);
                        member.setPassword(SecurityHelper.md5Encrypt(mobile + "_" + password));
                        factorySession.save(member);
                    }
                    request.getSession().setAttribute("member", member);
                    request.getSession().setAttribute("editorpath", "member/" + member.getId());
                    CookieHelper.SetCookie(response, "member_mobile", mobile, 604800, true);
                    if (request.getParameter("rememberpassword") != null) CookieHelper.SetCookie(response,
                            "member_password_" + mobile, password, 604800, true);
                    else CookieHelper.RemoveCookie(response, "member_password_" + mobile);
                    try {
                        String redirect_uri = request.getSession().getAttribute("member_redirect_uri") == null ?
                                "/" : request.getSession().getAttribute("member_redirect_uri").toString();
                        response.sendRedirect(redirect_uri);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            CookieHelper.RemoveCookie(response, "checkcode");
        }
    }

}
